//
// Created by fl on 23-9-5.
//

#ifndef SIGMA_GUIDANCE_ZMQ_CLIENT_H
#define SIGMA_GUIDANCE_ZMQ_CLIENT_H

#include <iostream>
#include <map>
#include <thread>
#include <regex>
#include <zmq.h>
#include <mutex>
#include "get_time.h"
#include <armadillo>
#include <atomic>

struct TargetInfo {
    uint8_t index;
    uint64_t time_stamp_ms;
    double lat;
    double lon;
    float alt;
    float spd_n;
    float spd_e;
    float spd_d;
};

typedef std::map<std::string, double> KeyValueMap;

class ZmqClient {
public:
    /**
     * 构造函数
     * @param ue4_ip 仿真机ip
     * @param ue4_port 仿真机通信端口，默认14552
     * @param interval_ms zmq请求周期，默认50ms
     */
    ZmqClient(std::string ue4_ip, uint32_t ue4_port = 14552,
              uint32_t interval_ms = 50);
    ~ZmqClient();

    // 目标位置1秒不更新时，返回false
    bool updated(TargetInfo &target);

    void request_msg(std::string str, bool req_once);

    int request_num(std::string str);

private:
    KeyValueMap get_target_map();

    int string_to_map(const std::string &data, KeyValueMap &kv_map);

private:
    KeyValueMap target_info_map_;

    std::string target_name_str_;

    uint64_t interval_us_{20000};

    std::atomic<bool> updated_{false};
    std::mutex udpdata_mtx_;

    void* context_;
    void* socket_;
};

#endif //SIGMA_GUIDANCE_ZMQ_CLIENT_H
